package com.tuanbiao.presenter.setting;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.hzjq.data.entity.SettingParams;
import com.hzjq.data.hzjq.entity.DeviceInfoEntity;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.constants.UserConstants;
import com.tuanbiao.controllers.SettingController;
import com.tuanbiao.events.SettingDeviceEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/5/7 17:00
 */
public class SettingDevicePresenter extends BasePresenter<SettingController.Model, SettingController.DeviceView> implements SettingController.DevicePresenter {
    private DeviceInfoEntity mDeviceInfoEntity;
    @UserConstants.KeyEventUserType
    private int mType;

    public SettingDevicePresenter(SettingController.Model model, SettingController.DeviceView rootView) {
        super(model, rootView);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void init(SettingDeviceEvent event) {
        mType = event.getType();
        EventBus.getDefault().removeStickyEvent(event);
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void getDeviceInfo() {
        Timber.i("getDeviceInfo:"+mType);
        mRootView.showType(mType);
        SettingParams settingParams = mModel.getSettingParams();
        mRootView.showModel(settingParams.isStandAloneModel());
        mModel.getDeviceInfo(entity -> {
            mDeviceInfoEntity = entity;
            mRootView.showDeviceNum(entity.getNum());
        });

    }

    @Override
    public void setModel(boolean keep, boolean isStandAloneModel) {
        Timber.i("setModel:" + keep);
        mModel.setModel(keep, isStandAloneModel);
        mRootView.showModel(isStandAloneModel);
        mRootView.showMessage("修改成功");
        if (!isStandAloneModel) {
            mRootView.startLogin();
        } else {
            mRootView.startMain();
        }
        mRootView.killMyself();
    }

//    @Override
//    public void showServerDialog() {
//        List<Integer> selected = new ArrayList<>();
//        List<UploadServiceBean> all = new ArrayList<>();
//        List<BlastServiceBean> blastServiceBeans = mModel.getAllUploadService();
//        for (int i = 0; i < LibCollections.size(blastServiceBeans); i++) {
//            BlastServiceBean bean = blastServiceBeans.get(i);
//            UploadServiceBean serviceBean = new UploadServiceBean();
//            serviceBean.setService(bean);
//            serviceBean.setServiceName(bean.getServiceName());
//            all.add(serviceBean);
//            if (bean.isSelect()) {
//                selected.add(i);
//            }
//        }
//        int[] ints = new int[LibCollections.size(selected)];
//        for (int i = 0; i < LibCollections.size(selected); i++) {
//            ints[i] = selected.get(i);
//        }
//        mRootView.showServiceDialog(all, ints);
//    }
//
//    @Override
//    public void changeService(HashMap<Integer, UploadServiceBean> data) {
//        if (!LibCollections.isEmpty(data)) {
//            List<BlastServiceBean>services = new ArrayList<>(LibCollections.size(data));
//            Set<Map.Entry<Integer, UploadServiceBean>> entries = data.entrySet();
//            for (Map.Entry<Integer, UploadServiceBean> entry : entries) {
//                services.add(entry.getValue().getService());
//            }
//
//            mModel.setUploadService(services);
//            Timber.i("changeService:" + data);
//            mRootView.showMessage("修改爆破记录服务器成功");
//        }
//    }
}
